package cn.arise.chat.ui.dialog;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.DatePicker;
import android.widget.TimePicker;

import com.lgmshare.component.app.LaraDialog;
import com.lgmshare.component.utils.UIUtils;
import com.lgmshare.k3.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;


public class DatetimePickerDialog extends LaraDialog {

    private int mMode;

    private Button dialog_btn_positive;
    private Button dialog_btn_negative;

    private View.OnClickListener mPositiveClickListener;
    private View.OnClickListener mNegativeClickListener;

    public DatetimePickerDialog(Context context) {
        super(context, R.style.CommonBottomSlideDialog);
    }

    @Override
    protected int onBindLayoutResId() {
        return R.layout.dialog_datetime;
    }

    @Override
    protected void initView() {
        setCancelable(true);
        setCanceledOnTouchOutside(true);

        Window window = getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        layoutParams.gravity = Gravity.BOTTOM;

        CalendarView calendarView = findViewById(R.id.calendarView);
        DatePicker datePicker = findViewById(R.id.dpPicker);
        TimePicker timePicker = findViewById(R.id.tpPicker);
        dialog_btn_negative = findViewById(R.id.dialog_btn_negative);
        dialog_btn_positive = findViewById(R.id.dialog_btn_positive);

        Calendar calendar = Calendar.getInstance();
        if (mMode == 0) {
            datePicker.setMinDate(calendar.getTimeInMillis() - 3600 * 24);
            datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {

                @Override
                public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    // 获取一个日历对象，并初始化为当前选中的时间
                    Calendar calendar = Calendar.getInstance();
                    calendar.set(year, monthOfYear, dayOfMonth);
                    SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日  HH:mm");

                }
            });
            datePicker.setVisibility(View.VISIBLE);

            timePicker.setIs24HourView(true);
            timePicker
                    .setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
                        @Override
                        public void onTimeChanged(TimePicker view, int hourOfDay,
                                                  int minute) {
                        }
                    });
            timePicker.setVisibility(View.VISIBLE);
        } else if (mMode == 1) {
            calendarView.setDate(calendar.getTimeInMillis());
            calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
                @Override
                public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
                    UIUtils.showToast("您选择的时间是：" + year + "年" + month + "月" + dayOfMonth + "日");
                }
            });
            calendarView.setVisibility(View.VISIBLE);
        } else if (mMode == 2) {
            datePicker.setMinDate(calendar.getTimeInMillis() - 3600 * 24);
            datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {

                @Override
                public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    // 获取一个日历对象，并初始化为当前选中的时间
                    Calendar calendar = Calendar.getInstance();
                    calendar.set(year, monthOfYear, dayOfMonth);
                    SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日  HH:mm");

                }
            });
            datePicker.setVisibility(View.VISIBLE);
        } else if (mMode == 3) {
            timePicker.setIs24HourView(true);
            timePicker
                    .setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
                        @Override
                        public void onTimeChanged(TimePicker view, int hourOfDay,
                                                  int minute) {
                        }
                    });
            timePicker.setVisibility(View.VISIBLE);
        }

        //######################按钮######################
        //右按钮(确定)
        if (mPositiveClickListener != null) {
            dialog_btn_positive.setOnClickListener(mPositiveClickListener);
        } else {
            dialog_btn_positive.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        }

        //左按钮（取消）
        if (mNegativeClickListener != null) {
            dialog_btn_negative.setOnClickListener(mNegativeClickListener);
        } else {
            dialog_btn_negative.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        }
    }

    public void setMode(int mode) {
        mMode = mode;
    }

    /**
     * 设置按钮（否定）
     *
     * @param onClickListener
     */
    public void setOnNegativeClick(final View.OnClickListener onClickListener) {
        mNegativeClickListener = onClickListener;
    }

    /**
     * 设置按钮（确定）
     *
     * @param onClickListener
     */
    public void setOnPositiveClick(final View.OnClickListener onClickListener) {
        mPositiveClickListener = onClickListener;
    }
}